package dt.inlee.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.TreeUtil;
import dt.inlee.domain.SysMenu;
import dt.inlee.system.service.SysMenuService;
import dt.inlee.system.vo.SysUserLoginBean;
import dt.inlee.system.vo.TreeNodeBean;

/**
 * 
 * 功能说明：根据登录用户ID显示该用户首页菜单
 * 
 * @作者： 张可贵 创建日期： 2010-5-13
 */
@Scope("prototype")
@Results({@Result(name = "success", type = "json")})
@Controller("menuTreeAction")
public class MenuTreeAction extends ActionSupport {
    private static Logger logger = Logger.getLogger(MenuTreeAction.class);
    private static final long serialVersionUID = -6784977600668791997L;
    @Autowired
    private SysMenuService sysMenuService;
    // 登陆用户信息
    private SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext.getContext().getSession()
            .get(CommonConstant.SESSION_USER);
    // 一级菜单的data，与节点的json格式相对应
    private String data;
    // 一级菜单的属性
    private Map<String, String> attr;
    // 一级菜单的所有子节点
    private List<TreeNodeBean> children;
    // 一级菜单ID
    private String parentId;
    // 菜单状态
    private String state;
    // 该用户对应的一级菜单list
    private List<Map<String, Object>> mainMenuList = new ArrayList<Map<String, Object>>();
    // easyui需要的jasontree
    @SuppressWarnings("unused")
    private JSONArray jsonTreeArray;

    public List<Map<String, Object>> getMainMenuList() {
        return mainMenuList;
    }

    public void setMainMenuList(List<Map<String, Object>> mainMenuList) {
        this.mainMenuList = mainMenuList;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public Map<String, String> getAttr() {
        return attr;
    }

    public void setAttr(Map<String, String> attr) {
        this.attr = attr;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public List<TreeNodeBean> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNodeBean> children) {
        this.children = children;
    }



    public String getContextPath() {
        return ServletActionContext.getServletContext().getContextPath();
    }

    /**
     * 根据选择的头部菜单，取得其相应的左侧菜单
     * 
     * @return
     */
    @Action(value = "getLeftMenu", results = {@Result(type = "json", params = {"excludeNullProperties", "true",
            "excludeProperties", "mainMenuList"})})
    @SkipValidation()
    public String findLeftMenu() throws FrameworkException, Exception {
        logger.debug("loginUser=" + loginUser.getId());
        List<SysMenu> sysMenus = this.sysMenuService.findSysMenuAndSubSysMenus(parentId, loginUser.getId());
        TreeNodeBean treeNodeBean =
                TreeUtil.convertMenuTreeNode(this.sysMenuService.findSysMenuById(parentId), sysMenus);
        children = treeNodeBean.getChildren();
        // 父节点
        data = treeNodeBean.getData();
        attr = treeNodeBean.getAttr();
        return SUCCESS;
    }

    /**
     * 取得头部菜单列表
     * 
     * @return
     */
    @Action(value = "initTopMenu", results = {@Result(type = "json", params = {"excludeNullProperties", "true",
            "excludeProperties", "attributes,children,data"})})
    @SkipValidation()
    public String initTopMenu() throws FrameworkException {
        mainMenuList = new ArrayList<Map<String, Object>>();
        // 字菜单
        List<Map<String, String>> subMenuList = null;
        List<SysMenu> list = null;
        List<SysMenu> sysMenus = this.sysMenuService.findOneLayerSysMenuByLoginUserId(loginUser.getId());
        for (SysMenu sysMenu : sysMenus) {
            Map<String, Object> oneLevel = new HashMap<String, Object>();
            oneLevel.put("menuId", sysMenu.getId());
            oneLevel.put("menuName", sysMenu.getName());
            /**
             * 以下是easyui所加的代码开始
             */
            subMenuList = new ArrayList<Map<String, String>>();
            list = sysMenu.getSysMenus();
            for (SysMenu sysMenu2 : list) {

                Map<String, String> twoLevel = new HashMap<String, String>();
                twoLevel.put("menuid", sysMenu2.getId());
                twoLevel.put("icon", sysMenu2.getNodeImage());
                twoLevel.put("menuname", sysMenu2.getName());
                twoLevel.put("url", sysMenu2.getPageUrl());
                subMenuList.add(twoLevel);
            }
            oneLevel.put("menus", subMenuList);
            /**
             * 以上是easyui所加的代码结束
             */
            mainMenuList.add(oneLevel);
        }
        return SUCCESS;
    }
}
